<?php

/**
 * @file
 * Contains theme override functions and preprocess functions for the theme.
 *
 * ABOUT THE TEMPLATE.PHP FILE
 *
 *   The template.php file is one of the most useful files when creating or
 *   modifying Drupal themes. You can add new regions for block content, modify
 *   or override Drupal's theme functions, intercept or make additional
 *   variables available to your theme, and create custom PHP logic. For more
 *   information, please visit the Theme Developer's Guide on Drupal.org:
 *   http://drupal.org/theme-guide
 *
 * OVERRIDING THEME FUNCTIONS
 *
 *   The Drupal theme system uses special theme functions to generate HTML
 *   output automatically. Often we wish to customize this HTML output. To do
 *   this, we have to override the theme function. You have to first find the
 *   theme function that generates the output, and then "catch" it and modify it
 *   here. The easiest way to do it is to copy the original function in its
 *   entirety and paste it here, changing the prefix from theme_ to core101_fundraising_.
 *   For example:
 *
 *     original: theme_breadcrumb()
 *     theme override: core101_fundraising_breadcrumb()
 *
 *   where core101_fundraising is the name of your sub-theme. For example, the
 *   zen_classic theme would define a zen_classic_breadcrumb() function.
 *
 *   If you would like to override any of the theme functions used in Zen core,
 *   you should first look at how Zen core implements those functions:
 *     theme_breadcrumbs()      in zen/template.php
 *     theme_menu_item_link()   in zen/template.php
 *     theme_menu_local_tasks() in zen/template.php
 *
 *   For more information, please visit the Theme Developer's Guide on
 *   Drupal.org: http://drupal.org/node/173880
 *
 * CREATE OR MODIFY VARIABLES FOR YOUR THEME
 *
 *   Each tpl.php template file has several variables which hold various pieces
 *   of content. You can modify those variables (or add new ones) before they
 *   are used in the template files by using preprocess functions.
 *
 *   This makes THEME_preprocess_HOOK() functions the most powerful functions
 *   available to themers.
 *
 *   It works by having one preprocess function for each template file or its
 *   derivatives (called template suggestions). For example:
 *     THEME_preprocess_page    alters the variables for page.tpl.php
 *     THEME_preprocess_node    alters the variables for node.tpl.php or
 *                              for node-forum.tpl.php
 *     THEME_preprocess_comment alters the variables for comment.tpl.php
 *     THEME_preprocess_block   alters the variables for block.tpl.php
 *
 *   For more information on preprocess functions and template suggestions,
 *   please visit the Theme Developer's Guide on Drupal.org:
 *   http://drupal.org/node/223440
 *   and http://drupal.org/node/190815#template-suggestions
 */

/**
 * Implementation of HOOK_theme().
 */
function core101_fundraising_theme(&$existing, $type, $theme, $path) {
    $hooks = zen_theme($existing, $type, $theme, $path);
    // Add your theme hooks like this:
    /*
      $hooks['hook_name_here'] = array( // Details go here );
     */
    // @TODO: Needs detailed comments. Patches welcome!
    return $hooks;
}

/**
 * Override or insert variables into all templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered (name of the .tpl.php file.)
 */
/* -- Delete this line if you want to use this function
  function core101_fundraising_preprocess(&$vars, $hook) {
  $vars['sample_variable'] = t('Lorem ipsum.');
  }
  // */

function core101_fundraising_taxonomy_term_page($tids, $result) {
    drupal_add_css(drupal_get_path('module', 'taxonomy') . '/taxonomy.css');

    $output = '';

    // Only display the description if we have a single term, to avoid clutter and confusion.
    if (count($tids) == 1) {
        $term = taxonomy_get_term($tids[0]);
        $description = $term->description;

//    // Check that a description is set.
//    if (!empty($description)) {
//      $output .= '<div class="taxonomy-term-description">';
//      $output .= filter_xss_admin($description);
//      $output .= '</div>';
//    }
    }

    $output .= taxonomy_render_nodes($result);

    return $output;
}

/**
 * Override or insert variables into the page templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
 */
function core101_fundraising_preprocess_page(&$vars, $hook) {
    global $base_url;
    $theme_root_path = $base_url . '/' . path_to_theme();
    $vars['theme_root_path'] = $theme_root_path;



    if (arg(0) == 'taxonomy' && is_numeric(arg(2))) {
        $term = taxonomy_get_term(arg(2));
        $vars['term'] = $term;
        $is_fist_level = _is_first_level_term($term->tid);
        if ($is_fist_level) {
            $vars['template_files'][] = 'page-taxonomy-term-first-level';
        } else {
            $vars['template_files'][] = 'page-taxonomy-term-product-level';
            //$vars['template_files'][] = 'page-taxonomy-term';
        }
    }
    core101_fundraising_removetab('Create new account', $vars);
    core101_fundraising_removetab('View', $vars);
    core101_fundraising_removetab('Orders', $vars);
    core101_fundraising_removetab('Track', $vars);
    core101_fundraising_removetab('Contact', $vars);
    core101_fundraising_removetab('Coupons', $vars);
    
}

function core101_fundraising_removetab($label, &$vars) {
  $tabs = explode("\n", $vars['tabs']);
  $vars['tabs'] = '';
 
  foreach ($tabs as $tab) {
    if (strpos($tab, '>' . $label . '<') === FALSE) {
      $vars['tabs'] .= $tab . "\n";
    }
  }
}

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */
function core101_fundraising_preprocess_node(&$vars, $hook) {
    //add sample_variable into node tpl
    //$vars['sample_variable'] = t('Lorem ipsum.');
    // Optionally, run node-type-specific preprocess functions, like
    // core101_fundraising_preprocess_node_page() or core101_fundraising_preprocess_node_story().
    $function = __FUNCTION__ . '_' . $vars['node']->type;
    if (function_exists($function)) {
        $function($vars, $hook);
    }
    /*
     * handle all of $_SESSION and $_GET
     */
    if (isset($_GET['referral_code'])) {
        $referral_code = core101_user_customization_decrypt($_GET['referral_code']);
        $args = explode('_', $referral_code);
        $_SESSION['org_id'] = get_org_nid_by_campaign_id($args[1]);
        //$_SESSION['org_id'] = $args[1];
        $_SESSION['referral_uid'] = $args[0];
        $_SESSION['referral_code'] = $_GET['referral_code'];
    }
}

function core101_fundraising_preprocess_node_product(&$vars, $hook) {
    if ($vars['teaser']) {
        $vars['template_files'][] = 'node-' . $vars['type'] . '-teaser';
    } else {
        $vars['template_files'][] = 'node-' . $vars['type'] . '-full';
    }
}

/**
 * Override or insert variables into the comment templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("comment" in this case.)
 */
/* -- Delete this line if you want to use this function
  function core101_fundraising_preprocess_comment(&$vars, $hook) {
  $vars['sample_variable'] = t('Lorem ipsum.');
  }
  // */

/**
 * Override or insert variables into the block templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("block" in this case.)
 */
/* -- Delete this line if you want to use this function
  function core101_fundraising_preprocess_block(&$vars, $hook) {
  $vars['sample_variable'] = t('Lorem ipsum.');
  }
  // */

function core101_fundraising_uc_cart_block_summary($item_count, $item_text, $total, $summary_links) {
    $context = array(
        'revision' => 'themed-original',
        'type' => 'amount',
    );
    // Build the basic table with the number of items in the cart and total.
//    $output = '<table class="cart-block-summary"><tbody><tr>'
//            . '<td class="cart-block-summary-items">' . $item_text . '</td>'
//            . '<td class="cart-block-summary-total"><label>' . t('Total:')
//            . '</label> ' . uc_price($total, $context) . '</td></tr>';
//
//    // If there are products in the cart...
//    if ($item_count > 0) {
//        // Add a view cart link.
//        $output .= '<tr class="cart-block-summary-links"><td colspan="2">'
//                . theme('links', $summary_links) . '</td></tr>';
//    }
//
//    $output .= '</tbody></table>';

    $output = '<h3>My cart : ' . uc_price($total, $context) . ' </h3>';
    $output .= '<p>' . theme('links', $summary_links) . '</p>';
    return $output;
}
